<!DOCTYPE html>

<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">

<title>Class: VBulletinRails::Session</title>

<link type="text/css" media="screen" href="../rdoc.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
</script>

<script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/navigation.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/search_index.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/search.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/searcher.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/darkfish.js"></script>


<body id="top" class="class">
<nav id="metadata">
  <nav id="home-section" class="section">
  <h3 class="section-header">
    <a href="../index.html">Home</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </h3>
</nav>


  <nav id="search-section" class="section project-section" class="initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <h3 class="section-header">
      <input type="text" name="search" placeholder="Search" id="search-field"
             title="Type to search, Up and Down to navigate, Enter to load">
    </h3>
  </form>

  <ul id="search-results" class="initially-hidden"></ul>
</nav>


  <div id="file-metadata">
    <nav id="file-list-section" class="section">
  <h3 class="section-header">Defined In</h3>
  <ul>
    <li>lib/vbulletin_rails/models/session.rb
  </ul>
</nav>

    
  </div>

  <div id="class-metadata">
    
    <nav id="parent-class-section" class="section">
  <h3 class="section-header">Parent</h3>
  
  <p class="link"><a href="../ActiveRecord/Base.html">ActiveRecord::Base</a>
  
</nav>

    
    <!-- Method Quickref -->
<nav id="method-list-section" class="section">
  <h3 class="section-header">Methods</h3>

  <ul class="link-list">
    
    <li><a href="#method-c-destroy">::destroy</a>
    
    <li><a href="#method-c-get">::get</a>
    
    <li><a href="#method-c-set">::set</a>
    
    <li><a href="#method-i-update_timestamps">#update_timestamps</a>
    
  </ul>
</nav>

  </div>

  <div id="project-metadata">
    <nav id="fileindex-section" class="section project-section">
  <h3 class="section-header">Pages</h3>

  <ul>
  
    <li class="file"><a href="../LICENSE.html">LICENSE</a>
  
    <li class="file"><a href="../TODO.html">TODO</a>
  
  </ul>
</nav>

    <nav id="classindex-section" class="section project-section">
  <h3 class="section-header">Class and Module Index</h3>

  <ul class="link-list">
  
    <li><a href="../Rails/Application/Configuration.html">Rails::Application::Configuration</a>
  
    <li><a href="../VBulletinRails.html">VBulletinRails</a>
  
    <li><a href="../VBulletinRails/Session.html">VBulletinRails::Session</a>
  
    <li><a href="../VBulletinRails/User.html">VBulletinRails::User</a>
  
    <li><a href="../ActionController/Base.html">ActionController::Base</a>
  
    <li><a href="../ActiveRecord/Base.html">ActiveRecord::Base</a>
  
  </ul>
</nav>

  </div>
</nav>

<div id="documentation">
  <h1 class="class">class VBulletinRails::Session</h1>

  <div id="description" class="description">
    
<p>Automatic class for handling VBulletin users sessions.</p>

  </div><!-- description -->

  
  
  
  <section id="5Buntitled-5D" class="documentation-section">
    

    

    
    <!-- Constants -->
    <section id="constants-list" class="section">
      <h3 class="section-header">Constants</h3>
      <dl>
      
        <dt id="PREFIX">PREFIX
        
        <dd class="description"><p>VBulletin tables prefix in database. It must set same as
<code>$config['Database']['tableprefix']</code> in your VBulletin forum</p>
        
      
        <dt id="VB_SESSION_TIMEOUT">VB_SESSION_TIMEOUT
        
        <dd class="description"><p>Timeout used to set <code>last_visit</code> in database. Taken from
VBulletin. Do not touch.</p>
        
      
      </dl>
    </section>
    

    

    <!-- Methods -->
    
     <section id="public-class-5Buntitled-5D-method-details" class="method-section section">
      <h3 class="section-header">Public Class Methods</h3>

    
      <div id="method-c-destroy" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">destroy</span><span
            class="method-args">(sessionhash)</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <p>Destroys session with given session hash.</p>
          

          
          <div class="method-source-code" id="destroy-source">
            <pre><span class="ruby-comment"># File lib/vbulletin_rails/models/session.rb, line 76</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">destroy</span> <span class="ruby-identifier">sessionhash</span>
  <span class="ruby-identifier">session</span> = <span class="ruby-identifier">find_by_sessionhash</span>(<span class="ruby-identifier">sessionhash</span>)
  <span class="ruby-identifier">session</span>.<span class="ruby-identifier">destroy</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">session</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- destroy-source -->
          
        </div>

        

        
      </div><!-- destroy-method -->

    
      <div id="method-c-get" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">get</span><span
            class="method-args">(options = {})</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <p>Returns <a href="Session.html">VBulletinRails::Session</a> object, needs
session hash and request</p>

<pre class="ruby"><span class="ruby-constant">VBulletinRails</span><span class="ruby-operator">::</span><span class="ruby-constant">Session</span>.<span class="ruby-identifier">get</span> :<span class="ruby-identifier">request</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">request</span>, :<span class="ruby-identifier">sessionhash</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">'f588c74c9d6e1c7ad05abf6bcae2186f'</span>
</pre>
          

          
          <div class="method-source-code" id="get-source">
            <pre><span class="ruby-comment"># File lib/vbulletin_rails/models/session.rb, line 63</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">get</span> <span class="ruby-identifier">options</span> = {}
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys</span>
  <span class="ruby-identifier">request</span> = <span class="ruby-identifier">check_request</span> <span class="ruby-identifier">options</span>
  <span class="ruby-identifier">idhash</span> = <span class="ruby-constant">VBulletinRails</span><span class="ruby-operator">::</span><span class="ruby-identifier">idhash</span>(<span class="ruby-constant">VBulletinRails</span><span class="ruby-operator">::</span><span class="ruby-identifier">fetch_alt_ip</span>(<span class="ruby-identifier">request</span>.<span class="ruby-identifier">headers</span>), <span class="ruby-identifier">request</span>.<span class="ruby-identifier">user_agent</span>)
  <span class="ruby-identifier">session</span> = <span class="ruby-identifier">find_by_sessionhash</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:sessionhash</span>].<span class="ruby-identifier">to_s</span>)

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">session</span>
    <span class="ruby-keyword">return</span> (<span class="ruby-identifier">session</span>.<span class="ruby-identifier">idhash</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">idhash</span> <span class="ruby-operator">&amp;&amp;</span> (<span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">session</span>.<span class="ruby-identifier">lastactivity</span>) <span class="ruby-operator">&lt;</span> <span class="ruby-constant">VB_SESSION_TIMEOUT</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">session</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">false</span>;
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- get-source -->
          
        </div>

        

        
      </div><!-- get-method -->

    
      <div id="method-c-set" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">set</span><span
            class="method-args">(options = {})</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <p>Sets user session for VBulletin. Needs <a
href="User.html">VBulletinRails::User</a> object, or email or username.
Controller <code>request</code> is mandatory!</p>

<pre class="ruby"><span class="ruby-constant">VBulletinRails</span><span class="ruby-operator">::</span><span class="ruby-constant">Session</span>.<span class="ruby-identifier">set</span> :<span class="ruby-identifier">request</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">request</span>, :<span class="ruby-identifier">user</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">vb_user</span>
<span class="ruby-constant">VBulletinRails</span><span class="ruby-operator">::</span><span class="ruby-constant">Session</span>.<span class="ruby-identifier">set</span> :<span class="ruby-identifier">request</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">request</span>, :<span class="ruby-identifier">email</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">'user@example.com'</span>
<span class="ruby-constant">VBulletinRails</span><span class="ruby-operator">::</span><span class="ruby-constant">Session</span>.<span class="ruby-identifier">set</span> :<span class="ruby-identifier">request</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">request</span>, :<span class="ruby-identifier">username</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">'username'</span>
</pre>
          

          
          <div class="method-source-code" id="set-source">
            <pre><span class="ruby-comment"># File lib/vbulletin_rails/models/session.rb, line 37</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">set</span> <span class="ruby-identifier">options</span> = {}
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys</span>
  <span class="ruby-identifier">request</span> = <span class="ruby-identifier">check_request</span> <span class="ruby-identifier">options</span>

  <span class="ruby-identifier">user</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-identifier">user</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:user</span>]
  <span class="ruby-identifier">user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find_by_email</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:email</span>]) <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:email</span>] <span class="ruby-keyword">and</span> <span class="ruby-identifier">user</span>.<span class="ruby-identifier">blank?</span>
  <span class="ruby-identifier">user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find_by_username</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:username</span>]) <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:username</span>] <span class="ruby-keyword">and</span> <span class="ruby-identifier">user</span>.<span class="ruby-identifier">blank?</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">VBulletinRailsException</span>, <span class="ruby-string">'User not found'</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">user</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">User</span>)

  <span class="ruby-identifier">nowstamp</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_i</span>
  <span class="ruby-identifier">alt_ip</span> = <span class="ruby-constant">VBulletinRails</span><span class="ruby-operator">::</span><span class="ruby-identifier">fetch_alt_ip</span>(<span class="ruby-identifier">request</span>.<span class="ruby-identifier">headers</span>)
  <span class="ruby-identifier">sessionhash</span> = <span class="ruby-constant">Digest</span><span class="ruby-operator">::</span><span class="ruby-constant">MD5</span>.<span class="ruby-identifier">hexdigest</span>((<span class="ruby-identifier">rand</span> * <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_i</span> * <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">usec</span>).<span class="ruby-identifier">to_s</span>)

  <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">execute</span>(<span class="ruby-node">&quot;INSERT INTO `#{table_name}`
                      (`sessionhash`, `userid`, `host`, `idhash`, `lastactivity`, `location`, `useragent`)
                      VALUES
                      ('#{sessionhash}', '#{user.userid}', '#{alt_ip}', '#{VBulletinRails::idhash(alt_ip, request.user_agent)}',
                      '#{nowstamp}', 'application', '#{request.user_agent}')&quot;</span>)

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">find_by_sessionhash</span>(<span class="ruby-identifier">sessionhash</span>)
<span class="ruby-keyword">end</span></pre>
          </div><!-- set-source -->
          
        </div>

        

        
      </div><!-- set-method -->

    
    </section><!-- public-class-method-details -->
  
     <section id="public-instance-5Buntitled-5D-method-details" class="method-section section">
      <h3 class="section-header">Public Instance Methods</h3>

    
      <div id="method-i-update_timestamps" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">update_timestamps</span><span
            class="method-args">()</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <p>Updates VBulletin session timestamp on the application side, to make them
consistent with VBulletin on the forum side.</p>
          

          
          <div class="method-source-code" id="update_timestamps-source">
            <pre><span class="ruby-comment"># File lib/vbulletin_rails/models/session.rb, line 19</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">update_timestamps</span>
  <span class="ruby-keyword">unless</span> <span class="ruby-identifier">user</span>.<span class="ruby-identifier">blank?</span>
    <span class="ruby-identifier">last_activity</span> = <span class="ruby-identifier">user</span>.<span class="ruby-identifier">lastactivity</span>
    <span class="ruby-identifier">nowstamp</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_i</span>
    <span class="ruby-identifier">last_visit</span> = (((<span class="ruby-identifier">nowstamp</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">last_activity</span>) <span class="ruby-operator">&gt;</span> <span class="ruby-constant">VB_SESSION_TIMEOUT</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">last_activity</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">user</span>.<span class="ruby-identifier">lastvisit</span>)
    <span class="ruby-identifier">update_attributes</span>(<span class="ruby-value">:lastactivity</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">nowstamp</span>)
    <span class="ruby-identifier">user</span>.<span class="ruby-identifier">update_attributes</span>(<span class="ruby-value">:lastactivity</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">nowstamp</span>, <span class="ruby-value">:lastvisit</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">last_visit</span>)
    <span class="ruby-keyword">return</span> [<span class="ruby-identifier">nowstamp</span>, <span class="ruby-identifier">last_visit</span>]
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">return</span> [<span class="ruby-value">0</span>, <span class="ruby-value">0</span>]
<span class="ruby-keyword">end</span></pre>
          </div><!-- update_timestamps-source -->
          
        </div>

        

        
      </div><!-- update_timestamps-method -->

    
    </section><!-- public-instance-method-details -->
  
  </section><!-- 5Buntitled-5D -->

</div><!-- documentation -->


<footer id="validator-badges">
  <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
  <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 3.11.
  <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
</footer>

